package gui;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;

import javax.swing.JPanel;

import data.Color;
import data.board.Square;
import data.pieces.*;

public class ChessboardPanel extends JPanel {
	private static final long serialVersionUID = 5628047125550350002L;
	private MainFrame frame;
	
	public ChessboardPanel(MainFrame f){
		super();
		
		this.frame = f;
		build();
	}

	private void build() {
		int size = frame.getSkin().square.white.getHeight();
		setPreferredSize(new Dimension(8*size, 8*size));
		setVisible(true);
	}
	
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		
	    Skin skin = frame.getSkin();
	    Piece p;
	    int size = skin.square.white.getHeight();
	    BufferedImage img;
	    SquareImg squareImg;
	    Square[][] squares = frame.getGame().getChessboard().getSquares();
	    
	    for(Square[] row : squares){
	    	for(Square s : row){
	    		img = (s.getColor() == Color.WHITE) ? skin.square.white : skin.square.black;
	    		g.drawImage(img, s.getX()*size, s.getY()*size, this);
	    		
	    		p = s.getPiece();
	    		if(p != null){
	    			if(p instanceof King){
	    				squareImg = skin.king;
	    			}else if(p instanceof Queen){
	    				squareImg = skin.queen;
	    			}else if(p instanceof Pawn){
	    				squareImg = skin.pawn;
	    			}else if(p instanceof Bishop){
	    				squareImg = skin.bishop;
	    			}else if(p instanceof Knight){
	    				squareImg = skin.knight;
	    			}else{
	    				squareImg = skin.rook;
	    			}
	    			img = (p.getCamp().getColor() == Color.WHITE) ? squareImg.white : squareImg.black;
		    		
	    			g.drawImage(img, s.getX()*size, s.getY()*size, this);
	    		}
	    	}
	    }
	    
	}

}
